package mesweb.mesweb.controller.device;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.Utils.TreeUtil;
import mesweb.mesweb.entity.device.MesDeviceType;
import mesweb.mesweb.service.impl.device.MesDeviceTypeServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author niufw
 * @since 2021-05-19
 */
@RestController
@RequestMapping("/mes/mes-device-type")
@Api(tags = "MesDeviceTypeController", description = "设备类型管理")
public class MesDeviceTypeController {

    @Autowired
    private MesDeviceTypeServiceImpl mesDeviceTypeService;

    @GetMapping("/GetAllDeviceTypes")
    @ApiOperation("获取设备类型列表")
    public CommonResult<List<MesDeviceType>> GetAllDeviceTypes(@ApiParam("设备类型名称") @RequestParam( value = "name",required = false) String name)
    {
        List<MesDeviceType> data = new ArrayList<>();
        if(StringUtils.isBlank(name) || name.equals("全部"))
        {
            data =  mesDeviceTypeService.list();
        }
        else {
            QueryWrapper<MesDeviceType> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("Device_Type_Name", name);
            data = mesDeviceTypeService.list(queryWrapper);
        }
        TreeUtil treeUtil = new TreeUtil();
        treeUtil.setMesDeviceTypeList(data);
        return  CommonResult.success(treeUtil.builTree());
    }

    @GetMapping("/GetDeviceType")
    @ApiOperation("根据编号获取设备类型")
    public CommonResult<MesDeviceType> GetDeviceTypeByNo(@ApiParam("设备类型编号") @RequestParam("no") String no)
    {
         if(no.isEmpty())
             return null;
         QueryWrapper<MesDeviceType> queryWrapper = new QueryWrapper<MesDeviceType>();
         queryWrapper.eq("Device_Type_No",no);
         return CommonResult.success(mesDeviceTypeService.getOne(queryWrapper));
    }
    @PostMapping("/SaveDeviceType")
    @ApiOperation("保存设备类型")
    public CommonResult<Boolean> SaveDeviceType(@ApiParam("设备类型") @RequestBody MesDeviceType mesDeviceType) {
        if (mesDeviceType == null)
            return null;
        try {
            QueryWrapper<MesDeviceType> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("device_type_No", mesDeviceType.getDeviceTypeNo());
            queryWrapper.or().eq("device_type_Name", mesDeviceType.getDeviceTypeName());
            MesDeviceType mesTechnologyFlow1 = mesDeviceTypeService.getOne(queryWrapper);
            if (mesTechnologyFlow1 != null) {
                String msg = StrUtil.format("编号{}或名称{}已经存在！", mesTechnologyFlow1.getDeviceTypeNo(), mesTechnologyFlow1.getDeviceTypeName());
                throw new ApiException(msg);
            }
            boolean result = mesDeviceTypeService.save(mesDeviceType);
            return CommonResult.success(result);
        } catch (Exception ex) {
            return CommonResult.failed(ex.getMessage());
        }
    }
    @DeleteMapping("/DeleteDeviceType/{Id}")
    @ApiOperation("删除设备类型")
    public CommonResult<Boolean> DeleteDeviceType(@ApiParam("设备类型ID") @PathVariable("Id") int Id) {
        try {
            boolean result = mesDeviceTypeService.removeById(Id);
            return CommonResult.success(result);
        } catch (Exception ex) {
            return CommonResult.failed(ex.getMessage());
        }
    }
}
